package com.flyme.util.test;

import com.flyme.common.poi.util.WordTemplateData;
import com.flyme.common.poi.util.WordTemplateUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 * Created by xinxingegeya on 2016/12/21.
 */
public class AppMain {

    public static void main(String args[]) throws IOException {

        WordTemplateData data = new WordTemplateData();
        data.addTableData("agency", "中华人民共和国中华人民共和国中华人民共和国中华人民共和国");
        data.addTableData("model", "车型这是");
        data.addTableData("color", "红色");
        data.addTableData("vin", "232323232323");  //车架号
        data.addTableData("title", "发票抬头"); //发票抬头
        data.addTableData("taxFlag", "是啊"); //是否一般纳税人
        data.addTableData("creditCode", "sd2323232323"); //社会信用代码
        data.addTableData("tonFlag", "是啊"); //是否填写吨位数
        data.addTableData("receipt01", "需盖章");
        data.addTableData("receipt02", "盖章");//抵扣联是否盖章;
        data.addTableData("receipt03", "盖章"); //报税联是否盖章
        data.addTableData("receipt04", "盖章"); //注册登记联是否盖章
        data.addTableData("receiptOther", "哈哈哈哈哈哈哈哈");
        String templatePath = "PickupCarLetter02.docx";
        XWPFDocument document = WordTemplateUtil.generateWordFromTemplate(templatePath, data);

        FileOutputStream fileOutputStream = null;
        URL path = Thread.currentThread().getContextClassLoader().getResource("");

        if (path == null) {
            return;
        }

        try {
            File file = new File(path.getPath(), "hello.docx");
            fileOutputStream = new FileOutputStream(file);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (document != null) {
                document.write(out);
            } else {
                return;
            }

            byte[] array = out.toByteArray();
            for (int i = 0; i < array.length; i++) {
                fileOutputStream.write(array[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }

    }
}
